cmake_minimum_required (VERSION 3.24)

include(cmake/setupkit.cmake)

set(NCTI_PROJECT ${NCTI_QT_LAUNCHER})

# 提取上一级目录的路径
get_filename_component(PARENT_DIR "${CMAKE_CURRENT_SOURCE_DIR}" DIRECTORY)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Qt自动处理
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# 查找Qt库
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Gui Widgets Network REQUIRED)

# 不使用Qt关键字
add_compile_definitions(QT_NO_KEYWORDS)

# Qt5-specific components
if(WIN32 AND QT_VERSION_MAJOR EQUAL 5)
  find_package(Qt5 COMPONENTS WinExtras REQUIRED)
endif()

# ----------------------------------
# NCTI库的包含目录
# ----------------------------------
include_directories(${INC_PATH_NCTI})
include_directories(${INC_PATH_OCC})
include_directories(${INC_PATH_PYTHON})

# 查找所有头文件目录
FUN_FIND_ALL_HEADFILE_DIR(${NCTI_APPLICATION_DIR_NCTICAX_INC} ${NCTI})
FUN_FIND_ALL_HEADFILE_DIR(${NCTI_APPLICATION_DIR_NCTICAX_INC} ${NCTI_BASE})
FUN_FIND_ALL_HEADFILE_DIR(${NCTI_APPLICATION_DIR_NCTICAX_INC} ${NCTI_RENDER})
FUN_FIND_ALL_HEADFILE_DIR(${NCTI_APPLICATION_DIR_NCTICAX_INC} ${NCTI_PLUGINMANAGER})
FUN_FIND_ALL_HEADFILE_DIR(${NCTI_APPLICATION_DIR_NCTICAX_INC} ${NCTI_GEOM_ENGINE})
FUN_FIND_ALL_HEADFILE_DIR(${NCTI_APPLICATION_DIR_NCTI_TEST_INC} ${NCTI_WINDOW})
FUN_FIND_ALL_HEADFILE_DIR(${NCTI_APPLICATION_DIR_NCTI_TEST_INC} ${NCTI_OBJECT})
FUN_FIND_ALL_HEADFILE_DIR(${NCTI_APPLICATION_DIR_NCTI_TEST_INC} ${NCTI_PLUGIN})
FUN_FIND_ALL_HEADFILE_DIR(${NCTI_APPLICATION_DIR_NCTI_TEST_INC} ${NCTI_PUBFUN})
FUN_FIND_ALL_HEADFILE_DIR(${NCTI_APPLICATION_DIR_NCTI_TEST_INC} ${NCTI_QT_LAUNCHER})
FUN_FIND_ALL_HEADFILE_DIR(${CMAKE_CURRENT_SOURCE_DIR} "")

# 生成目标
add_executable(${NCTI_PROJECT})

# 源码扫描
file(GLOB_RECURSE SrcFiles
    "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h"
    "${CMAKE_CURRENT_SOURCE_DIR}/src/*.ui"
)
source_group_by_dir(SrcFiles ${CMAKE_CURRENT_SOURCE_DIR})
set(PROJECT_SOURCES ${SrcFiles})

# 添加源文件到目标
target_sources(${NCTI_PROJECT} PRIVATE 
  ${PROJECT_SOURCES} 
  ${CMAKE_CURRENT_SOURCE_DIR}/resources/resource.qrc
)

# 添加NCTI库目录
target_link_directories(${NCTI_PROJECT} PRIVATE
	${LIBS_PATH}
	${LIB_PATH_NCTI}
	${LIB_PATH_OCC}
	${LIB_PATH_PYTHON}
)

# 添加SARibbon库的引用
set(QT_RIBBONBAR_ROOT "${NCTI_3RDPARTY_ROOT}/Source/SARibbonBar")
target_include_directories(${NCTI_PROJECT} PRIVATE ${QT_RIBBONBAR_ROOT}/src)
 
# 添加第三方头文件目录
target_include_directories(${NCTI_PROJECT} PRIVATE 
  ${NCTI_3RDPARTY_INC}
  ${NCTI_3RDPARTY_INC}/ormpp
  ${NCTI_3RDPARTY_INC}/sqlite3
)

# 添加第三方库目录
target_link_directories(${NCTI_PROJECT} PRIVATE
    ${NCTI_3RDPARTY_LIB}
)

# 启动器默认是否与GEPLab共用，涉及到recentFiles.db的生成位置
option(ENABLE_LAUNCHER_BIND_TO_PRE "Enable the launcher bound to GEPLab" ON)
if(ENABLE_LAUNCHER_BIND_TO_PRE)
  target_compile_definitions(${NCTI_PROJECT} PRIVATE 
    NCTI_LAUNCHER_BIND_TO_PRE
  )
endif()

target_compile_definitions(${NCTI_PROJECT} PRIVATE 
  ORMPP_ENABLE_SQLITE3
  #ORMPP_ENABLE_LOG
)

# Add custom manifest files to an executable target.
function(add_windows_manifest manifest_path target)
  set(exe_manifest "${manifest_path}/${target}.exe.manifest")
  set(compat_manifest "${manifest_path}/compatibility.manifest")
  
  if(NOT EXISTS "${exe_manifest}")
    message(FATAL_ERROR "Main manifest file not found: ${main_manifest}")
  endif()
  
  if(NOT EXISTS "${compat_manifest}")
    message(FATAL_ERROR "Compatibility manifest file not found: ${compat_manifest}")
  endif()

  add_custom_command(
    TARGET ${target}
    POST_BUILD
    COMMAND mt.exe -nologo
            -manifest "${exe_manifest}" "${compat_manifest}"
            -outputresource:$<TARGET_FILE:${target}>;#1
    COMMENT "Embedding manifest file into ${target}"
    VERBATIM 
    )
endfunction()

if(WIN32)
  # 添加 Unicode 字符集支持
  target_compile_definitions(${NCTI_PROJECT} PRIVATE UNICODE _UNICODE)
	
  add_win_rc(${NCTI_PROJECT} ICON ${CMAKE_CURRENT_SOURCE_DIR}/resources/app/app.ico)

  # Adding manifest.
  add_windows_manifest(${CMAKE_CURRENT_SOURCE_DIR} ${NCTI_PROJECT})

  # Link Qt5-specific libraries
  if(QT_VERSION_MAJOR EQUAL 5)
    target_link_libraries(${NCTI_PROJECT} PUBLIC Qt5::WinExtras)
  endif()
endif()

# Qt库链接
target_link_libraries(${NCTI_PROJECT} PUBLIC
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Gui
    Qt${QT_VERSION_MAJOR}::Widgets
    Qt${QT_VERSION_MAJOR}::Network
)

# 第三方库链接
target_link_libraries(${NCTI_PROJECT} PRIVATE
  ${NCTI_QT_RIBBONBAR}
  sqlite3
)

# NCTI核心库链接
target_link_libraries(${NCTI_PROJECT} PRIVATE 
  ${NCTI} 
  ${NCTI_BASE} 
  ${NCTI_RENDER} 
  ${NCTI_WINDOW} 
  ${NCTI_PLUGINMANAGER} 
  ${NCTI_PUBFUN} 
  ${NCTI_PLUGIN} 
  ${NCTI_OBJECT}
)